use strict;

if($#ARGV<1){
    die "syntax: fit_subset.pl file t0 t1";
}
my ($file,$t0,$t1)=@ARGV;
if(!-e $file){
    print "file $file does not exist\n";
    exit;
}
my @lines=`cat $file`;
chomp(@lines);
my $maxt=$#lines;
if($t1<$maxt){
    $maxt=$t1;
}
my $invy0=1/$lines[$t0];
open(OUT,">gdp.dat") or die "$!\n";
for(my $t=$t0;$t<=$maxt;$t++){
    print OUT $lines[$t]*$invy0,"\n";
}
close OUT;
my $fitLine=`./Fit 0.5 0.1 -0.1`;
print "$fitLine";
chomp($fitLine);
my @tabl=split(/ +/,$fitLine);
my ($f,$lp,$lm)=@tabl;

open(OUT,">$file-$t0-$t1.dat") or die "$!\n";

for(my $t=$t0;$t<=$maxt+5;$t++){
    print OUT "$t ",$lines[$t0]*($f*exp($lp*($t-$t0))+(1-$f)*exp($lm*($t-$t0))),"\n";
}
close OUT;
